package com.hg.admin.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.hg.admin.db.dto.UserAddDTO;
import com.hg.admin.service.UserGroupService;
import com.hg.admin.service.UserService;
import com.hg.admin.vo.VO4UserGroup;
import com.hg.admin.vo.VO4userAdd;
import com.hg.admin.vo.VO4userOperation;
import com.hg.util.action.MbaseAction;
import com.hg.util.common.Sec;

public class UserAction extends MbaseAction
{
	UserService us = new UserService();
	UserGroupService ugs = new UserGroupService();
	public void index(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		String is_json = request.getParameter("is_json");
		if("1".equals(is_json)){
			//非
			Map<String, Object> data = us.index(getSearchArgs(root, 30," order by create_time desc"));
			map.put("total", data.get("total"));
			map.put("rows", data.get("rows"));
			json_return(root, data);
		}else{
			//第一次访问表格,返回界面
			List<VO4UserGroup> groups = ugs.getAll();
			
			root.put("groups", groups);
			
			ftl_return(root);
		}
	}
	
	public void add(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		List<VO4UserGroup> groups = ugs.getAll();
		root.put("groups", groups);
		ftl_return(root);
	}
	
	public void edit(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		String id_str = request.getParameter("id");
		int id = Integer.parseInt(id_str);
		UserAddDTO dto = us.getOne(id);
		root.put("id", id_str);
		root.put("dto", dto);
		List<VO4UserGroup> groups = ugs.getAll();
		root.put("groups", groups);
		ftl_return(root);
	}
	
	public void update(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		String id = request.getParameter("id");
		String user_name = request.getParameter("user_name");
		String user_pwd = request.getParameter("user_pwd");
		String mobile = request.getParameter("mobile");
		String group_id = request.getParameter("group_id");
		String is_effect = request.getParameter("is_effect");
		
		String psw = Sec.MD5Encrypt(user_pwd, 32);//MD5加密
		UserAddDTO dto = new UserAddDTO();
		dto.setId(Integer.parseInt(id));
		dto.setUser_name(user_name);
		dto.setUser_pwd(psw);
		dto.setMobile(mobile);
		dto.setGroup_id(Integer.parseInt(group_id));
		dto.setIs_effect(Integer.parseInt(is_effect));
		
		
		if(us.updateUser(dto)){
			map.put("err_code", -1);
			map.put("err_msg", "修改成功");
		}else{
			map.put("err_code", 1101);
			map.put("err_msg", "操作失败");
		}
		
		
		json_return(root, map);
	}
	
	public void insert(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		String name = request.getParameter("user_name");
		String password = request.getParameter("user_pwd");
		String mobile = request.getParameter("mobile");
		String level = request.getParameter("group_id");
		String state = request.getParameter("is_effect");
		
		VO4userAdd vo = new VO4userAdd();
		vo.setName(name);
		vo.setPassword(password);
		vo.setMobile(mobile);
		vo.setLevel(level);
		vo.setState(state);
		if(us.userAdd(vo)){
			map.put("err_code", -1);
			map.put("err_msg", "添加成功");
		}else{
			map.put("err_code", 1101);
			map.put("err_msg", "添加失败");
		}
		
		
		json_return(root, map);
	}
	
	public void account(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		String id = request.getParameter("id");
		root.put("id", id);
		ftl_return(root);
	}
	public void updataAccount(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		HttpSession session = request.getSession();
		
		String id = request.getParameter("id");
		String fx_money = request.getParameter("fx_money");
		String money = request.getParameter("money");
		String score = request.getParameter("score");
		String desc = request.getParameter("desc");
		
		VO4userOperation vo = new VO4userOperation();
		vo.setU_id(id);
		vo.setAdmin_id((String)session.getAttribute("userId"));
		vo.setAdmin_name((String)session.getAttribute("username"));
		vo.setFx_money(fx_money);
		vo.setMoney(money);
		vo.setScore(score);
		vo.setDesc(desc);
		
		if(us.updataAccount(vo)){
			map.put("err_code", -1);
			map.put("err_msg", "添加成功");
		}else{
			map.put("err_code", 1101);
			map.put("err_msg", "添加失败");
		}
		
		json_return(root, map);
	}
	
	
	public void del(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		String id_str = request.getParameter("id");
		int id = Integer.parseInt(id_str);
		
		if(us.del(id)){
			map.put("err_code", -1);
			map.put("err_msg", "删除成功");
		}else{
			map.put("err_code", 1101);
			map.put("err_msg", "操作失败");
		}
		
		
		json_return(root, map);
	}
	
	public void dels(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		String ids = request.getParameter("ids");
		
		if(us.dels(ids)){
			map.put("err_code", -1);
			map.put("err_msg", "删除成功");
		}else{
			map.put("err_code", 1101);
			map.put("err_msg", "操作失败");
		}
		
		
		json_return(root, map);
	}
}
